package wxk.interview.serializable;

import java.io.*;

/**
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/9.
 * @since 2.2
 */
public class PersonExt implements Externalizable {

    private static final long serialVersionUID = -829124411442646432L;
    private String idNo;
    private String name;
    private transient Integer age;

    public PersonExt(){
        System.out.println("non args constructor");
    }

    public PersonExt(String idNo, String name, Integer age) {
        System.out.println("with args constructor");
        this.idNo = idNo;
        this.name = name;
        this.age = age;
    }

    public String getIdNo() {
        return idNo;
    }

    public void setIdNo(String idNo) {
        this.idNo = idNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    private void writeObject(ObjectOutputStream out) throws IOException{
        out.defaultWriteObject();
        out.writeInt(age);
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        age = in.readInt();
    }

    @Override
    public String toString() {
        return "Person{" +
                "idNo='" + idNo + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(this);
        out.writeInt(age);
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        in.readObject();
        this.age = in.readInt();
    }
}
